<?php

namespace view;

require_once("Model/loginModel.php");

class loginView {

	/**
	 * @var loginModel
	 */
	private $model;

	/**
	 * @var string
	 */
	private $message;

	private static $password = "password";
	private static $username = "username";
	private static $login = "login";
	private static $logout = "logout";
	private static $remember = "remember";

	/**
	 * initiate class member variable
	 */
	public function __construct(\model\loginModel $model) {
		$this->model = $model;
	}

	/**
	 * @param String $message
	 * @return String HTML
	 */
	private function HTMLform($message ="") {
		$HTMLCode = 
		"<h2>Ej Inloggad</h2>
		<form action='?login' method='post'>
	        <fieldset>
	        	<legend>Login - Skriv in användarnamn och lösenord</legend>
	        	<p>".$message."</p>
	        	<label>Användarnamn:<input type='text'";       	
	        	if (!empty($_POST[self::$username])) {
        				$HTMLCode .="value="
        				.htmlentities($_POST[self::$username])
        				." name='".self::$username."'/></label>";
        			}
        			else{ $HTMLCode .= " name='".self::$username."'/></label>"; }
    	$HTMLCode .= "<label>Lösenord:<input type='password' name='".self::$password."'/>
					  </label>
	        	<label>Håll mig inloggad:
	        		<input type='checkbox' name='remember' value='1' />
	        	</lable>
	        	<input type='submit' value='Logga in'/>	        	
	        </fieldset>
        </form>";
        return $HTMLCode;
	}

	/**
	 * @return String HTML
	 */
	private function HTMLLoggedIn($message ="") {
		$HTMLCode ="<h2>".
			$this->loginUser(). " är inloggad </h2>
			<p>". $message ."</p>
	    	<a href='?logout'>Logga ut</a><br/>";
			
		return $HTMLCode;
	}

	/**
	 * @return Boole
	 */
	public function loginUser() {
		if (isset($_POST[self::$username])) {
			return $_POST[self::$username];
		}
		else if (isset($_COOKIE[self::$username])) {
			return $_COOKIE[self::$username];
		}		
	}

	/**
	 * @return Boole
	 */
	public function loginPass() {
		if (isset($_POST[self::$password])) {
			return $_POST[self::$password];
		}
		else if (isset($_COOKIE[self::$password])) {
			return $_COOKIE[self::$password];
		}
	}

	/**
	 * @return Boole
	 */
	public function hasUserLoggedOut() {
		if (isset($_GET[self::$logout])) {
			return true;
		}		
	}

	/**
	 * @return Boole
	 */
	public function hasUserLoggedIn() {
		if (isset($_GET[self::$login])) {
			return true;
		}
	}	

	/**
	 * Set the username cookie and password
	 * @return boole
	 */
	public function createLoginCookie($postUser, $postPass) {
		setcookie(self::$username, $postUser, $this->model->cookieDeath);
		setcookie(self::$password, $postPass, $this->model->cookieDeath);
	}

	

	/**
	 * @return boole
	 */
	public function hasUserCookieLoggedIn() {
		if (isset($_POST[self::$remember])) {
			return true;
		}
	}

	public function isCookieSet() {
		if (isset($_COOKIE[self::$username]) && isset($_COOKIE[self::$password])) {
			return true;
		}
	}

	/**
	 * destroy and remove cookies on logout
	 * @return void
	 */
	public function logout() {
		$this->message = "<p>Du har nu loggat ut</p>";
		$this->removeCookies();
	}

	/**
	 * remove cookies
	 * @return void
	 */
	private function removeCookies() {
		setcookie(self::$username, "", time()-3600);
		setcookie(self::$password, "", time()-3600);
	}

	/**
	 * Create and return datestring
	 * @return string
	 */
	private function getDate() {
		setlocale(LC_TIME, "Swedish");		
		$day = utf8_encode(strftime("%A, "));
		$dayNr = strftime("den %d ");
		$month = strftime("%B");
		$year = strftime(" år %Y.");
		$time = strftime(" Klockan är [%X]");		

		return ucfirst($day)
		.$dayNr
		.ucfirst($month)
		.$year
		.$time;
	}

	/**
	 * Set output message to the proper error
	 * @return void
	 */
	public function badLogin() {
		if ($this->isCookieSet()) {
			$this->message = "<p>Felaktig information i cookie</p>";
			$this->removeCookies();
		} else { 			
			if ($this->loginUser() == "") {
				$this->message .= "<p>Användarnamn saknas</p>";
			} else if ($this->loginPass() == "") {
				$this->message .= "<p>Lösenord saknas</p>";
			} else {
				$this->message = "<p>Felaktigt användarnamn och/eller lösenord</p>";
			}
		}
	}

	public function goodLogin() {
		if (!$this->model->checkSession()) {
			if ($this->hasUserCookieLoggedIn() || 
			$this->isCookieSet()) {
				if ($this->isCookieSet()) {
					$this->message  = "<p>Inloggning lyckades via cookies</p>";
				} 
				else {
					$this->message  = "<p>Inloggning lyckades och vi kommer ihåg dig nästa gång</p>";
				}			
			} else {
				$this->message  = "<p>Inloggning lyckades</p>";
			} 
		}
	}

	/**
	 * Assemble and return output HTML
	 * @param  String $message
	 * @return String html
	 */
	public function output() {
		$message = $this->message;
		if ($this->model->userLoggingOut) {
			$output = $this->HTMLform($message);
			$this->getDate();	
		}
		else if (!$this->model->userLoggedIn) {
			$this->removeCookies();
			$output = $this->HTMLform($message).
			$this->getDate();
		}
		else {
			$this->model->setSession();
			$output = $this->HTMLLoggedIn($message).
			$this->getDate();
		}
		return $output;
	}
}